package me.zixing.java.chapter04.task01;

import java.util.Random;
import java.util.Scanner;

/**
 * Created by bingh on 2017/10/12.
 */
public class ElementFinder {

    private int[] pandoraBox = new int[10];

    public static void main(String[] args){
        ElementFinder elementFinder = new ElementFinder();

        elementFinder.fullPandoraBox();
        int num = elementFinder.getInputNum();

        for (int i=0;i<elementFinder.pandoraBox.length;i++){
            System.out.println(elementFinder.pandoraBox[i]);
            if(num == elementFinder.pandoraBox[i]){
                System.out.println(String.format("啊哈！%d在数组中，是第%d个元素（从1开始）",num,i + 1));
                return;
            }
        }

        System.err.println("找不到输入元素");

    }

    //随机填充
    public void fullPandoraBox(){
        Random random = new Random();

        for (int i=0;i<pandoraBox.length;i++) {
            pandoraBox[i] = random.nextInt(20);
            System.out.println(pandoraBox[i]);
        }
    }

    //获取输入
    public int getInputNum(){
        System.out.println("请输入一个整数：");
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        return num;
    }

}
